﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel;
using Microsoft.Practices.Unity;
namespace P2.Cuberry.Modules.SimpleAccounting.FiCheque
{
    class FiChequesTransactionButtonView : MenuItem, IFiChequesTransactionButtonView
    {
        IUnityContainer _container;
        public FiChequesTransactionButtonView(IUnityContainer container)
        {
            _container = container;
            RegionContext.GetObservableContext(this).PropertyChanged += this.RegionContextChanged;
            // attach button icon
            var img = new System.Windows.Controls.Image { Source = new BitmapImage(new Uri("/P2.Cuberry.Modules.SimpleAccounting.FiCheque;component/Images/cheque4.png", UriKind.Relative)) };
            img.Width = 29;
            img.Height = 16;
            this.Icon = img;
            // attach button header
            this.Header = P2.Cuberry.Framework.Helper.P2Translator.GetResource("FiChequeOpenBtn");

        }
        private void RegionContextChanged(object sender, PropertyChangedEventArgs e)
        {
            string param = (string)RegionContext.GetObservableContext(this).Value;
            if (this.Presenter != null)
            {
                // only for safety - it would never be null
                this.Command = this.Presenter.FiChequeOpenCommand;
                if (string.IsNullOrEmpty(param))
                {
                    this.Presenter.FiTransactionItemID = null;
                    this.Presenter.FiscalYearID = null;
                }
                else
                {
                    var arrparam = param.Split('|');
                    this.Presenter.FiscalYearID = int.Parse(arrparam[0]);
                    if (arrparam.Length >= 2 && !string.IsNullOrEmpty(arrparam[1]))
                    {
                        var fiTransactionItemID = int.Parse(arrparam[1]);
                        if (fiTransactionItemID == -1)
                        { // new transactionitem
                            this.Presenter.FiTransactionItemID = int.Parse(arrparam[2]);
                        }
                        else
                        {
                            this.Presenter.FiTransactionItemID = int.Parse(arrparam[1]);

                        }
                        
                    }
                    else
                    {
                        this.Presenter.FiTransactionItemID = null;
                    }
                }
            }


        }

        public FiChequesTransactionButtonPresentationModel Presenter
        {
            get
            {
                if (this.DataContext == null || !(this.DataContext is FiChequesTransactionButtonPresentationModel))
                    this.DataContext = _container.Resolve<FiChequesTransactionButtonPresentationModel>();
                return (FiChequesTransactionButtonPresentationModel)this.DataContext;
            }
            set
            {
                this.DataContext = value;
            }
        }

    }
}
